﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Se7en.Admin.Common;
using Se7en.Admin.Business;
using ERMSys.Framework.Utilities;
using dal = Se7en.Admin.Model;

namespace Se7en.Admin.Web.Controls
{
    public partial class MenuEdit : UserControlBase
    {
        #region Properties

        private List<int> ListParentId
        {
            get
            {
                if (this.ViewState["ListParentId"] == null)
                    return new List<int>();
                return this.ViewState["ListParentId"] as List<int>;
            }
            set
            {
                this.ViewState["ListParentId"] = value;
            }
        }

        protected int? MenuParentId
        {
            get
            {
                return (ViewState["menuParentId"] != null ? (int?)ViewState["menuParentId"].ConvertToInt32() : null);
            }
            set
            {
                ViewState["menuParentId"] = value;
            }
        }

        protected int MenuId
        {
            get
            {
                return (ViewState["MenuID"] ?? "").ConvertToInt32();
            }
        }

        #endregion

        #region GUI Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            lblErrorModal.Text = "";
        }

        public void LoadBlankMenuData()
        {
            txtMenuLabel.Text = "";
            txtDescription.Text = "";
            txtUrl.Text = "";

            int total;
            ddlparentMenu.Items.Clear();
            ddlparentMenu.DataTextField = "Title";
            ddlparentMenu.DataValueField = "MenuID";
            ddlparentMenu.DataSource = this.DBContext.Menu_GetAll(-1, -1, out total);
            ddlparentMenu.DataBind();

            ddlparentMenu.Items.Insert(0, new ListItem() { Text = "Choose Parent Menu", Value = "-1" });
        }

        public void LoadMenuData(int menuId)
        {
            dal.Menu menu = this.DBContext.Menu_GetById(menuId);
            if (menu != null)
            {
                txtMenuLabel.Text = menu.Title;
                txtDescription.Text = menu.Description;
                txtUrl.Text = menu.Url;

                int total;
                ddlparentMenu.Items.Clear();
                ddlparentMenu.DataTextField = "Title";
                ddlparentMenu.DataValueField = "MenuID";
                ddlparentMenu.DataSource = this.DBContext.Menu_GetAll(-1, -1, out total);
                ddlparentMenu.DataBind();
                ddlparentMenu.Items.Insert(0, new ListItem() { Text = "Choose Parent Menu", Value = "-1" });

                if (menu.ParentID != null)
                    ddlparentMenu.Items.FindByValue(menu.ParentID.Value.ToString()).Selected = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.ValidateData())
            {
                this.ShowMsg(lblErrorModal, dal.MessageType.Warning);
                return;
            }

            dal.Menu menu = new dal.Menu();

            if (this.Mode == dal.Modes.Edit)
            {
                menu = this.DBContext.Menu_GetById(btnSave.CommandArgument.ConvertToInt32());
            }
            else if (this.Mode == dal.Modes.Add)
            {
                menu.Order = this.DBContext.Menu_GetMaxOrder() + 1;
                this.DBContext.Menu_AddNew(menu);
            }
            if (ddlparentMenu.SelectedValue.ConvertToInt32() != -1)
                menu.ParentID = ddlparentMenu.SelectedValue.ConvertToInt32();
            menu.Title = txtMenuLabel.Text.Trim();
            menu.Description = txtDescription.Text.Trim();
            menu.Url = txtUrl.Text.Trim();
            try
            {
                this.DBContext.SaveChanges();
                this.OnSuccess(sender, e);
            }
            catch (Exception ex)
            {
            }
            //try
            //{

            //}
            //catch
            //{
            //    this.OnFail(sender, e);
            //}
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            OnCancel(sender, e);
        }

        private bool ValidateData()
        {
            bool isValid = true;

            return isValid;
        }


        #endregion
    }
}
